import { message } from 'antd';
export default {
  namespace: 'db',

  state: {
    serverUserInfo: [], // 服务器上的用户信息
    mainAlarmInfoTable: [], // 报警信息表,显示到前端的报警信息
  },

  // 和后台交互
  effects: {
    *saveMainAlarmInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_save_main_alarm_info', [payload]);
      if (response === 'ok') {
        message.success('保存成功!', 3);
      } else {
        message.error(response, 5);
      }
      yield put({
        type: 'getMainAlramInfo',
      });
    },
    *updateFanzhuanCalcArgs({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_update_fanzhuan_calc_args', []);
      if (response === 'ok') {
        message.success('更新成功!', 3);
      } else {
        message.error("更新失败," + response, 5);
      }
    },

    *updateCalcArgs({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_update_calc_args', []);
      if (response === 'ok') {
        message.success('更新成功!', 3);
      } else {
        message.error("更新失败," + response, 5);
      }
    },

    *getMainAlramInfo({ payload }, { call, put }) {
      const alarmInfoTableVal = yield call(window.eel.run_py, 'db', 'web_request_get_main_alarm_info_table', []);
      yield put({
        type: 'updateMainAlarmInfoTable',
        payload: alarmInfoTableVal,
      });
    },
    *getUserInfo({ payload }, { call, put }) {
      const userInfo = yield call(window.eel.run_py, 'db', 'web_request_get_user_info', []);
      yield put({
        type: 'updateServerUserInfo',
        payload: userInfo,
      });
    },

    *saveUserInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_save_user_info', [payload]);
      yield put({
        type: 'getUserInfo',
      });
      if (response === 'ok') {
        message.success('保存成功', 3);
      } else {
        message.error('保存失败,数据库异常' + response, 3);
      }
    },

    *deleteUserInfo({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_delete_user_info', [
        payload,
      ]);
      yield put({
        type: 'getUserInfo',
      });
    },

    *dumpDatabase({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_dump_data_base', []);
      if (response === 'ok') {
        message.success('数据库备份成功!', 3);
      } else {
        message.error(response, 5);
      }
    },

    *importDatabase({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_import_data_base', []);
      if (response === 'ok') {
        message.success('数据库导入成功!', 3);
      } else {
        message.error(response, 5);
      }
    },

    *openDatabaseDumpDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_open_database_dump_dir', []);
      if (response === 'ok') {
        message.success('打开备份目录成功!', 3);
      } else {
        message.error(response, 5);
      }
    },

    *openDatabaseImportDir({ payload }, { call, put }) {
      const response = yield call(window.eel.run_py, 'db', 'web_request_open_databaes_import_dir', []);
      if (response === 'ok') {
        message.success('打开导入目录成功!', 3);
      } else {
        message.error(response, 5);
      }
    },
  },

  // 状态更新
  reducers: {
    updateMainAlarmInfoTable(state, action) {
      return {
        ...state,
        mainAlarmInfoTable: action.payload,
      };
    },
    updateServerUserInfo(state, action) {
      return {
        ...state,
        serverUserInfo: action.payload,
      };
    },
  },
};
